package com.mall.b2c.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.b2c.bean.GoodsCategoryBean;
import com.mall.b2c.dao.GoodsCategoryDao;
import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.b2c.service.GoodsCategoryService;
import com.mall.common.bean.Order;
import com.mall.common.bean.Order.Direction;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * @author wangzx
 * Service实现类 - 商品
 */
@Service("b2cGoodsCategoryService")
@Transactional(readOnly=true)
public class GoodsCategoryServiceImpl extends BaseServiceImpl<GoodsCategoryDomain, Long> implements GoodsCategoryService {
	
	
	@Resource
	public void setBaseDao(GoodsCategoryDao goodsCategoryDao) { 
		super.setBaseDao(goodsCategoryDao); 
	}

	@Override
	@Transactional(readOnly=true)
	@Cacheable(value="b2cGoodsCategory", key="'goodsCategory'+#parentId+','"
			+ "+#excludeId+','+#isRecommend+','+#isTop+','+#count+','"
			+ "+#orderBy")
	public List<GoodsCategoryDomain> findList(Long parentId,
				Long excludeId,Boolean isRecommend,Boolean isVisible, 
				Boolean isTop, Integer count, Order orderBy) {
		String hql = "from b2cGoodsCategoryDomain bean where 1 = 1";
		
		Finder finder = Finder.create(hql);
		if(parentId != null){
			finder.append(" and bean.parent.id = :parentId");
			finder.setParam("parentId", parentId);
		}else if(isTop!=null && isTop){
			finder.append(" and bean.parent is null");
		}
		
		if(excludeId != null){
			finder.append(" and bean.id != :excludeId");
			finder.setParam("excludeId", excludeId);
		}
		
		if(isRecommend != null){
			finder.append(" and bean.recommend = :recommend");
			finder.setParam("recommend", isRecommend);
		}
		
		if(isVisible != null){
			finder.append(" and bean.showInNav = :showInNav");
			finder.setParam("showInNav", isVisible);
		}
		
		if(orderBy != null){
			finder.setOrder(orderBy);
		}
		
		if(count != null){
			finder.setMaxResults(count);
		}
		return finder.findHQLList();
	}

	@Override
	@CacheEvict(value="b2cGoodsCategory",allEntries=true)
	@Transactional(propagation=Propagation.REQUIRED)
	public void persist(GoodsCategoryDomain entity) {
		super.persist(entity);
	}

	@Override
	@CacheEvict(value="b2cGoodsCategory",allEntries=true)
	@Transactional(propagation=Propagation.REQUIRED)
	public void remove(Long[] ids) {
		super.remove(ids);
	}

	@Override
	@CacheEvict(value="b2cGoodsCategory",allEntries=true)
	@Transactional(propagation=Propagation.REQUIRED)
	public GoodsCategoryDomain updateByUpdater(GoodsCategoryDomain entity) {
		return super.updateByUpdater(entity);
	}

	@Override
	@Cacheable(value="b2cGoodsCategory", key="'goodsCategory'+#id")
	public GoodsCategoryDomain getCategoryById(Long id) {
		return find(id);
	}

	@Override
	@Cacheable(value="b2cGoodsCategory", key="'goodsCategory'+#catIds+','+#isVisible+','+#parentId+','+#isRecommend+','+#num+','+#orderBy")
	public List<GoodsCategoryBean> getGoodsCategoryList(Long[] catIds, Boolean isVisible,
			Long parentId, Boolean isRecommend, Integer num, String orderBy) {
		Finder finder = Finder.create("select new com.mall.b2c.bean.GoodsCategoryBean(bean) from GoodsCategoryDomain bean where 1=1");
		if(!ArrayUtils.isEmpty(catIds)){
			finder.append(" and bean.id in (:catIds)");
			finder.setParam("catIds", catIds);
		}
		if(parentId == null){
			finder.append(" and bean.parent is null");
		}else{
			finder.append(" and bean.parent.id = :parentId");
			finder.setParam("parentId", parentId);
		}
		if(isVisible != null){
			finder.append(" and bean.showInNav = :showInNav");
			finder.setParam("showInNav", isVisible);
		}
		if(isRecommend != null){
			finder.append(" and bean.recommend = :isRecommend");
			finder.setParam("isRecommend", isRecommend);
		}
		if(num != null){
			finder.setMaxResults(num);
		}
		if(StringUtils.isNotBlank(orderBy)){
			String fieldName = orderBy.split(" ")[0];
			String direction  = orderBy.split(" ")[1];
			finder.setOrder(new Order(fieldName, Direction.valueOf(direction)));
		}
		return finder.findHQLList();
	}

}
